//
//  WSPurchaseController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/10.
//

#import "WSPurchaseController.h"
#import "CTMediator+WSPurchaseAction.h"

@interface WSPurchaseController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btnArray;
@property (weak, nonatomic) IBOutlet UIView *buttonView;
@property (nonatomic, strong) NSMutableDictionary *contentVCDict;
@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, strong) UIView * lineView;
@end

@implementation WSPurchaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewLayout];
    [self setContentVCWithIndex:self.selectIndex];
}

- (void)viewLayout {
    [self.buttonView addSubview:self.lineView];
    [self.scrollView setContentSize:CGSizeMake(kScreenWidth * 2, self.contentHeight)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self btnAction:self.btnArray[self.selectIndex]];
    });
}

- (void)layoutLineView:(UIButton *)btn {
    CGPoint center = self.lineView.center;
    center.x = btn.center.x;
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.center = center;
    }];
}

- (UIViewController *)addSubscribeController {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Subscribe];
    target.view.frame = CGRectMake(0, 0, WSScreenWidth, self.contentHeight);
    return target;
}

- (UIViewController *)addRechargeController {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Recharge];
    target.view.frame = CGRectMake(WSScreenWidth, 0, WSScreenWidth, self.contentHeight);
    return target;
}

// 获取並且添加子控制器
- (void)setContentVCWithIndex:(NSInteger)index {
    BOOL animated = YES;
    if(self.contentVCDict.count == 0) {
        animated = NO;
    }
    BaseController *vc = self.contentVCDict[@(index)];
    if(!vc) {
        if (index == 0) {
            vc = self.addSubscribeController;
        }
        if (index == 1) {
            vc = self.addRechargeController;
        }
        vc.view.frame = self.scrollView.bounds;
        vc.view.mj_x = WSScreenWidth * index;
        vc.view.mj_y = 0;
        [self.scrollView addSubview:vc.view];
        [self addChildViewController:vc];
        self.contentVCDict[@(index)] = vc;
    }
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:animated];
}

- (void)setBtnStatusWithIndex:(NSInteger)index {
    for (UIButton * btn in self.btnArray) {
        btn.selected = NO;
        btn.selected = btn.tag == index;
        if (btn.tag == index) {
            [self layoutLineView:btn];
        }
    }
}

- (IBAction)btnAction:(UIButton *)sender {
    [self setBtnStatusWithIndex:sender.tag];
    if (self.scrollView.scrollEnabled) {
        [self setContentVCWithIndex:sender.tag];
    }
}


#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x / WSScreenWidth;
        [self setBtnStatusWithIndex:x];
        [self setContentVCWithIndex:x];
    }
}

- (NSMutableDictionary *)contentVCDict {
    if (!_contentVCDict) {
        _contentVCDict = [[NSMutableDictionary alloc]init];
    }
    return _contentVCDict;
}

- (CGFloat)contentHeight {
    if (_contentHeight <= 0) {
        _contentHeight = WSScreenHeight - self.view.safeAreaInsets.top - 20 -44;
    }
    return _contentHeight;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor colorFromHexRGB:@"F8D6BD"];
        _lineView.frame = CGRectMake(0, 44 - 3, 12, 3);
        _lineView.layer.cornerRadius = 1.5;
    }
    return _lineView;
}
@end
